package com.zhouyu.fileSync.FScommon.api;

import com.alibaba.fastjson.JSONObject;
import com.zhouyu.fileSync.FScommon.api.entities.Message;
import com.zhouyu.fileSync.FScommon.api.entities.Request;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.FileRegion;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.CharsetUtil;

import java.nio.CharBuffer;
import java.util.List;

/**
 * Created by zhouyu@lightinthebox.com on 2019/8/14
 **/
public class JSONEncoder extends MessageToByteEncoder<Message> {


    protected void encode(ChannelHandlerContext channelHandlerContext, Message msg, ByteBuf out) throws Exception {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer();
        byte[] bytes = JSONObject.toJSONBytes(msg);
        byteBuf.writeBytes(bytes);
        byteBuf.writeBytes(Unpooled.copiedBuffer("#_@_#".getBytes()));
        out.writeBytes(byteBuf);
    }
}
